package br.cefetrn.banco;

import java.awt.HeadlessException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import javax.swing.JOptionPane;

import utilidades.FormatarData;

import br.cefetrn.dominio.Declaracao;
import br.cefetrn.dominio.Responsavel_Declaracao;
import br.cefetrn.fachada.FachadaResponsavel_Declaracao;

public class Responsavel_DeclaracaoDAO implements FachadaResponsavel_Declaracao {

	@SuppressWarnings("finally")
	public int insertResponsavel_DeclaracaoP(Responsavel_Declaracao res) throws ClassNotFoundException, IOException {

		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		int cont = 0;

		try {
			System.out.println("entrou no try do banco");
			conn = Banco.getConexao();
			stmt = conn.prepareStatement("insert into responsavel_declaracao (cpf, nome, ddd, tel, ramal, fax, email)" +
					" values (?, ?, ?, ?, ?, ?, ?)");
			stmt.setString(1, res.getCpf());
			stmt.setString(2, res.getNome().trim());
			stmt.setInt(3, res.getDdd());
			stmt.setInt(4, res.getTel());
			stmt.setInt(5, res.getRamal());
			stmt.setInt(6, res.getFax());
			stmt.setString(7, res.getEmail());
			stmt.executeUpdate();
			cont = 1;
		}
		catch (SQLException ex) {
			cont = 0;
			ex.printStackTrace();
		}
		finally {
			System.out.println("entrou no finally do banco");
			Banco.closeConexao(conn, rs, stmt);
			return cont;
		}
	}
	
    public Responsavel_Declaracao selectResp_Declaracao(String cpf) throws ClassNotFoundException, IOException {
        
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        Responsavel_Declaracao resp = null;
        
        try {
            conn = Banco.getConexao();
            stmt = conn.prepareStatement("select * from responsavel_declaracao where cpf = ?");
            stmt.setString(1, cpf);
            rs = stmt.executeQuery();
            while (rs.next()){
                resp = new Responsavel_Declaracao();
                resp.setCpf(rs.getString("cpf"));
                resp.setDdd(rs.getInt("ddd"));
                resp.setEmail(rs.getString("email"));
                resp.setFax(rs.getInt("fax"));
                resp.setNome(rs.getString("nome"));
                resp.setRamal(rs.getInt("ramal"));
                resp.setTel(rs.getInt("tel"));                
            }
        }
        catch (SQLException ex) {
        	ex.printStackTrace();
        }
        finally {
        Banco.closeConexao(conn, rs, stmt);
        return resp;
        }
    }
    
    @SuppressWarnings("finally")
	public int deleteResponsavel_Declaracao(String cpf) throws ClassNotFoundException, IOException {
        
        Connection conn = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        int res = 0;
        
        try {
            conn = Banco.getConexao();
            stmt = conn.prepareStatement("delete from responsavel_declaracao where cpf = ?");            
            stmt.setString(1, cpf);
            stmt.executeUpdate();
            res = 1;
        }
        catch (SQLException ex) {
        	res = 0;
        	ex.printStackTrace();        	
        }
        finally {
	        Banco.closeConexao(conn, rs, stmt);
	        System.out.println(res);
	        return res;
        }
    }   

}
